package cn.lingyangwl.agile.system.controller;

import cn.lingyangwl.agile.infra.*;
import cn.lingyangwl.agile.infra.module.group.model.rqrs.*;
import cn.lingyangwl.agile.model.enums.*;
import cn.lingyangwl.agile.ram.*;
import cn.lingyangwl.agile.system.model.entity.*;
import cn.lingyangwl.agile.system.model.rqrs.diy.*;
import cn.lingyangwl.agile.system.service.*;
import cn.lingyangwl.framework.core.response.*;
import cn.lingyangwl.framework.security.xss.core.*;
import cn.lingyangwl.framework.validation.group.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 表单
 * 
 * @author shenguangyang
 */
@Api(tags = "表单")
@RestController
@RequestMapping("/diyForm")
public class DiyFormController {
    @Resource
    private DiyFormService diyFormService;
    @Resource
    private DiyFormFieldService diyFormFieldService;
    @Resource
    private InfraApi infraApi;

    @ApiOperation(value = "分页表单")
    @GetMapping("/page")
    public Resp<IPage<DiyFormResp>> pageDiyForm(@Validated DiyFormQueryReq req) {
        return Resp.ok(diyFormService.pageDiyForm(req));
    }

    @ApiOperation(value = "通过id查找表单")
    @GetMapping("/get")
    public Resp<DiyFormResp> getDiyForm(@RequestParam(value = "id") Long id) {
        return Resp.ok(diyFormService.getDiyForm(id));
    }
  
    @ApiOperation(value = "保存表单")
    @PostMapping("/save")
    @XssCleanIgnore({ "formConfig" })
    public Resp<?> saveDiyForm(@Validated(InsertGroup.class) @RequestBody DiyFormSaveReq req) {
        return Resp.ok(diyFormService.saveDiyForm(req));
    }
  
    @ApiOperation(value = "更新表单")
    @PostMapping("/update")
    @XssCleanIgnore({ "formConfig" })
    public Resp<?> updateDiyForm(@Validated(UpdateGroup.class) @RequestBody DiyFormSaveReq req) {
        return Resp.ok(diyFormService.updateDiyForm(req));
    }
  
    @ApiOperation(value = "批量删除表单")
    @DeleteMapping("/batchDelete")
    public Resp<?> batchDeleteDiyForm(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(diyFormService.batchDeleteDiyForm(ids));
    }
  
    @ApiOperation(value = "删除表单")
    @DeleteMapping("/delete")
    public Resp<?> deleteDiyForm(@RequestParam(value = "id") Long id) {
        return Resp.ok(diyFormService.deleteDiyForm(id));
    }


    @ApiOperation(value = "删除表单组")
    @DeleteMapping("/deleteGroup")
    @RamResource(action = "diy:formGroup:delete", name = "删除表单组", type = ActionType.WRITE)
    public Resp<?> deleteGroup(@RequestParam(value = "id") Long id) {
        DataGroupDeleteReq req = new DataGroupDeleteReq();
        req.setBizType(DataGroupBizTypeEnum.DIY_FORM.getType());
        req.setIds(Collections.singletonList(id));
        return Resp.ok(diyFormService.deleteGroup(req));
    }

    @ApiOperation(value = "批量删除表单组")
    @DeleteMapping("/batchDeleteGroup")
    @RamResource(action = "diy:formGroup:delete", name = "删除表单组", type = ActionType.WRITE)
    public Resp<?> batchDeleteGroup(@RequestParam(value = "ids") List<Long> ids) {
        DataGroupDeleteReq req = new DataGroupDeleteReq();
        req.setBizType(DataGroupBizTypeEnum.DIY_FORM.getType());
        req.setIds(ids);
        return Resp.ok(diyFormService.deleteGroup(req));
    }

    @ApiOperation(value = "保存表单组")
    @PostMapping("/saveGroup")
    @RamResource(action = "diy:formGroup:save", name = "保存表单组", type = ActionType.WRITE)
    public Resp<Long> saveDataGroup(@RequestBody DataGroupSaveReq req) {
        req.setBizType(DataGroupBizTypeEnum.DIY_FORM.getType());
        return Resp.ok(infraApi.saveDataGroup(req));
    }

    @ApiOperation(value = "更新表单组")
    @PostMapping("/updateGroup")
    @RamResource(action = "diy:formGroup:update", name = "更新表单组", type = ActionType.WRITE)
    public Resp<?> updateDataGroup(@RequestBody DataGroupUpdateReq req) {
        return Resp.ok(infraApi.updateDataGroup(req));
    }

    @ApiOperation(value = "查询表单字段")
    @GetMapping("/listField")
    public Resp<List<DiyFormField>> listFormField(@RequestParam("formId") Long formId) {
        return Resp.ok(diyFormFieldService.listField(Collections.singletonList(formId)));
    }

    @ApiOperation(value = "导入表")
    @PostMapping("/importTable")
    @RamResource(action = "diy:form:importTable", name = "导入数据库表到表设计中", type = ActionType.WRITE)
    public Resp<?> importTable(@RequestBody DiyFormImportTableReq req) {
        diyFormService.importTable(req);
        return Resp.ok();
    }
}
